<template>
    <el-container class="print_list_container">
        <vAside ref="printAside"
                :options="asideOptions"
                :treeOptions="treeOptions"
                :treeData="treeData"
                is-show-header
                @event="handleAsideEvent"
                style="height: 100%">
            <template slot="button" v-if="showAside">
                <el-tooltip class="item" effect="dark" content="全部" placement="top">
                    <i class="ml10 fa fa-list-ul" :style="listStyle" @click="getList(true, true)" />
                </el-tooltip>
            </template>
        </vAside>
        <el-main>
            <el-card class="box-card">
                <headerTemplate slot="header" ref="header"
                                :table-ref="tableOptions.ref"
                                :toolbar_buttons="headerOption.toolbar_buttons"
                                :toolbar_filter="headerOption.toolbar_filter"
                                :searchForm="searchForm"
                                @event="handleHeaderEvent">

                </headerTemplate>
                <div class="card_container">
                    <vxe-table-template :data="dataList" :options.sync="tableOptions"
                                        @event="handleTableEvent" />
                </div>
            </el-card>
        </el-main>
    </el-container>
</template>

<script>
import { mapState } from "vuex";
import PrintForm from "../dialog/print/PrintForm";
import provide from "@common/mixins/provide";

export default {
    extends: provide,
    name: "index",
    provide() {
        return {
            setChildrenRef: (name, ref) => {
                this[name] = ref;
            },
            getChildrenRef: (name) => {
                return this[name];
            },
            getRef: () => {
                return this;
            },
        };
    },
    data() {
        const _this = this;
        return {
            asideOptions: {
                width: '150px',
                title: '归属功能',
            },
            treeOptions: {
                ref: "$PrintTypeTree",
                highlightCurrent: true,
                defaultExpandAll: false,
                nodeKey: 'value',
                props: {
                    label: "display_text",
                    children: "children",
                },
            },
            treeData: [],
            treeNodeKey: {},
            headerOption: {
                toolbar_buttons: [
                    {
                        id: "refresh",
                        icon: "el-icon-refresh-right",
                        show: true,
                        method: () => {
                            _this.getList();
                        },
                    },
                    {
                        id: "add",
                        icon: "fa fa-plus",
                        type: "primary",
                        show: true,
                        method: () => {
                            _this.openDialog(null, 'add');
                        },
                    },
                    {
                        id: "edit",
                        icon: "fa fa-pencil",
                        type: "success",
                        show: true,
                        method: () => {
                            _this.openDialog(null, 'edit');
                        },
                    },
                    {
                        id: "delete",
                        icon: "fa fa-trash-o",
                        type: "danger",
                        show: true,
                        method: () => {
                            _this.deleteConfirm();
                        },
                    },
                ],
            },
            searchForm: {},
            dataList: [],
            selectionList: [],
            tableOptions: {
                ref: "PrintListTable",
                container: ".print_list_container",
                multiSelect: true,
                columns: [
                    {
                        label: "归属功能",
                        prop: "type",
                        align: "center",
                        width: 100,
                        formatter: (row) => {
                            let filter = _this.printTypeList.filter(item => item.value === row.type);
                            if (_this.$utils.isNotEmptyList(filter)) {
                                return filter[0].display_text;
                            }
                            return '';
                        },
                    },
                    {
                        label: "模板编码",
                        prop: "code",
                        align: "center",
                    },
                    {
                        label: "模板名称",
                        prop: "name",
                        align: "center",
                    },
                    {
                        label: "状态",
                        prop: "state",
                        align: "center",
                        width: 100,
                        cellRender: {
                            name: 'ElSwitch',
                            props: {
                                activeValue: 1,
                                inactiveValue: 0,
                            },
                            events: {
                                change: _this.saveRow,
                            },
                        },
                    },
                    {
                        label: "排序",
                        prop: "sn",
                        align: "center",
                        width: 100,
                    },
                ],
                operates: {
                    list: [
                        {
                            id: "edit",
                            icon: "fa fa-pencil",
                            type: "primary",
                            size: "mini",
                            show: true,
                            tooltip: {
                                content: "编辑",
                            },
                            method: (index, row) => {
                                _this.openDialog(row, 'edit');
                            },
                        },
                        {
                            id: "copy",
                            icon: "fa fa-copy",
                            type: "primary",
                            size: "mini",
                            show: true,
                            tooltip: {
                                content: "复制",
                            },
                            method: (index, row) => {
                                _this.openDialog(row, 'copy');
                            },
                        },
                        {
                            id: "design",
                            icon: "el-icon-scissors",
                            type: "success",
                            size: "mini",
                            show: true,
                            tooltip: {
                                content: "模板设计",
                            },
                            method: (index, row) => {
                                _this.design(row);
                            },
                        },
                        {
                            id: "delete",
                            icon: "fa fa-trash-o",
                            type: "danger",
                            size: "mini",
                            show: true,
                            tooltip: {
                                content: "删除",
                            },
                            method: (index, row) => {
                                _this.deleteConfirm(row);
                            },
                        },
                    ],
                },
                pagination: {
                    pageIndex: 1,
                },
            },
        };
    },
    computed: {
        ...mapState('base', [ 'printTypeList' ]),
        type() {
            return this.$route.params.type;
        },
        showAside() {
            return !this.$utils.isNotEmpty(this.type);
        },
        listStyle() {
            let style = {};
            if (this.$utils.isNotEmptyList(this.printTypeList) && !this.$utils.isNotEmpty(this.treeNodeKey.sid)) {
                style.color = '#409EFF';
            }
            return style;
        },
    },
    mounted() {
        this.init();
    },
    methods: {
        async init() {
            if (!this.$utils.isNotEmptyList(this.printTypeList)) {
                await this.$store.dispatch('base/initPrintTypeList');
            }
            this.initTreeData();
            this.getList(true);
        },
        initTreeData() {
            const _this = this;
            let filter = _this.printTypeList.map(item => $.extend({}, item));
            if (_this.$utils.isNotEmpty(_this.type)) {
                filter = filter.filter(item => item.value === _this.type);
            }
            _this.treeData = filter;
            if (_this.$utils.isNotEmpty(_this.type)) {
                _this.treeNodeKey = $.extend({}, filter[0]);
                _this.$nextTick(() => {
                    if (_this.$PrintTypeTree) {
                        _this.$PrintTypeTree.setCurrentKey(_this.treeNodeKey.value);
                    }
                });
            }
        },
        handleAsideEvent(event) {
            if (event.type === "nodeClick") {
                this.treeNodeKey = event.data;
                this.getList(true);
            }
        },
        handleHeaderEvent(event) {
            this.searchForm = $.extend({}, this.searchForm, event.searchForm);
            if (event.type === "search") {
                this.getList(true);
            } else if (event.type === "refresh") {
                this.getList(true);
            }
        },
        getList(search, allTree) {
            const _this = this;
            if (allTree) {
                _this.treeNodeKey = {};
            }
            _this.dataList = [];
            if (search) {
                _this.tableOptions = _this.resetTablePagination(_this.tableOptions);
            }
            let queryData = _this.getQueryParams(_this.tableOptions, _this.searchForm);
            if (_this.$utils.isNotEmpty(_this.type)) {
                queryData.type = _this.type;
            } else if (_this.$utils.isNotEmpty(_this.treeNodeKey.sid)) {
                queryData.type = _this.treeNodeKey.value;
            }
            _this.fetchData(_this.$api.API_FindBasePrintDesigns, queryData).then(result => {
                _this.dataList = result.data || [];
                _this.tableOptions = _this.setTableTotal(_this.tableOptions, result);
            }).catch(() => {
            });
        },
        handleTableEvent(event) {
            if (event.type === 'page') {
                this.getList();
                return;
            }
            if (event.type === 'selectionChange') {
                this.selectionList = event.value;
            }
        },
        saveRow({ row }) {
            const _this = this;
            let subData = $.extend({}, row);
            _this.fetchData(_this.$api.API_SaveBasePrintDesign, subData).then(() => {
                _this.$message.success('保存成功！');
            }).catch(() => {
            });
        },
        openDialog(row, type) {
            const _this = this;
            let title = type === 'add' ? '创建' : '编辑';
            if (type === 'copy') {
                title = '复制';
            }
            if (!row) {
                if (type === 'add') {
                    row = {};
                    if (_this.showAside) {
                        row.type = _this.treeNodeKey.value || '';
                    } else {
                        row.type = _this.type;
                    }
                }
                if (type === 'edit') {
                    if (!_this.checkSelection(_this.selectionList)) {
                        return;
                    }
                    row = $.extend({}, _this.selectionList[0]);
                }
            }
            _this.$dialog({
                title,
                width: '400px',
                component: {
                    value: PrintForm,
                    props: {
                        type,
                        row,
                        showType: _this.showAside,
                    },
                    event(event, dialog) {
                        console.log(event, dialog);
                    },
                },
                confirmed() {
                    _this.getList();
                },
            });
        },
        //删除
        deleteConfirm(row) {
            const _this = this;
            let sids = [];
            if (row) {
                sids.push(row.sid);
            } else {
                if (!_this.checkSelection(_this.selectionList)) {
                    return;
                }
                sids = _this.selectionList.map(item => item.sid);
            }
            _this.$confirm("是否继续删除？", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                _this.fetchData(_this.$api.API_DeleteBasePrintDesigns, {
                    sids
                }).then(() => {
                    _this.$message.success("删除成功");
                    _this.getList();
                }).catch(() => {
                });
            }).catch(() => {
            });
        },
        design(row) {
            this.$router.push({
                path: '/printDesign',
                query: {
                    sid: row.sid,
                    name: row.name,
                },
            });
        },
    },
}
</script>

<style lang="less" rel="stylesheet/less" scoped>
.print_list_container {
    width: 100%;
    height: 100%;
    overflow: hidden;
}
</style>
